/**
 * 图表高阶组件
 * 封装echarts，实现图表的resize和销毁操作
 * */

import React, {PureComponent} from 'react'
// import {changeScale} from './util/common'
export default function (Component) {
    class Base extends PureComponent {
        constructor() {
            super()
        }

        setChartInstance = (chart) => {
            this.chart = chart
            this.listenDomResize()
            return this.chart
        }

        resize = ()=>{
            if (this.chart) {
                this.chart.resize()
            }
        }

        /*监听resize事件*/
        listenDomResize = () => {
            window.addEventListener('resize',this.resize)
        }

        componentWillUnmount() {
            this.chart && this.chart.dispose()
            window.removeEventListener('resize',this.resize)
        }

        render() {
            return <Component {...this.props} setChartInstance={this.setChartInstance}/>
        }
    }
    return Base
}
